package org.carl.code;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.StrUtil;
import org.carl.code.core.PlatForm;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author Administrator
 */
public abstract class Generate implements PlatForm {
    /**
     * 源文件
     */
    protected File sourceFile;
    /**
     * 目标文件
     */
    protected File targetFile;

    public Generate(File sourceFile, File targetFile) {
        this.sourceFile = sourceFile;
        this.targetFile = targetFile;
    }

    /**
     * 生成方法
     */
    public abstract void generate();

    /**
     * 获取源内容
     * @return
     */
    public String getSourceContent(){
        return FileReader.create(sourceFile).readString();
    }

    /**
     * 获取目标内容
     * @param replaceFlag
     * @return
     */
    public String getTargetContent(boolean replaceFlag){
        String content = FileReader.create(sourceFile).readString();
        if(replaceFlag){
            return content.replaceAll(sourceName.get(),targetName.get());
        }
        return content;
    }
    public void touchTargetFile(){
        FileUtil.touch(targetFile);
    }
    public void fillTargetFile(boolean replaceFlag){
        fillTargetFile(replaceFlag,null);
    }
    public void fillTargetFile(boolean replaceFlag, Function<String,String> function){
        touchTargetFile();
        String content = getTargetContent(replaceFlag);
        if(function!=null){
            content=function.apply(content);
        }
        FileUtil.appendUtf8String(content,targetFile);
    }

    /**
     * 首字母大写
     * @param letter
     * @return
     */
    public static String upperFirstLatter(String letter){
        char[] chars = letter.toCharArray();
        if(chars[0]>='a' && chars[0]<='z'){
            chars[0] = (char) (chars[0]-32);
        }
        return new String(chars);
    }
}
